namespace DotNetNuke.Services.Scheduling.DNNScheduling
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Threading;

    public class DNNScheduler : SchedulingProvider
    {
        private const string ProviderType = "schedulingprovider";

        public override int AddSchedule(ScheduleItem objScheduleItem)
        {
            Scheduler.CoreScheduler.RemoveFromScheduleQueue(objScheduleItem);
            int i = new SchedulingController().AddSchedule(objScheduleItem.TypeFullName, objScheduleItem.TimeLapse, objScheduleItem.TimeLapseMeasurement, objScheduleItem.RetryTimeLapse, objScheduleItem.RetryTimeLapseMeasurement, objScheduleItem.RetainHistoryNum, objScheduleItem.AttachToEvent, objScheduleItem.CatchUpEnabled, objScheduleItem.Enabled, objScheduleItem.ObjectDependencies, objScheduleItem.Servers);
            ScheduleHistoryItem objScheduleHistoryItem = new ScheduleHistoryItem(objScheduleItem);
            objScheduleHistoryItem.NextStart = DateAndTime.Now;
            objScheduleHistoryItem.ScheduleID = i;
            if (((((objScheduleHistoryItem.TimeLapse != Null.NullInteger) && (objScheduleHistoryItem.TimeLapseMeasurement != Null.NullString)) && objScheduleHistoryItem.Enabled) ? 1 : 0) != 0)
            {
                objScheduleHistoryItem.ScheduleSource = ScheduleSource.STARTED_FROM_SCHEDULE_CHANGE;
                Scheduler.CoreScheduler.AddToScheduleQueue(objScheduleHistoryItem);
            }
            DataCache.RemoveCache("ScheduleLastPolled");
            return i;
        }

        public override void DeleteSchedule(ScheduleItem objScheduleItem)
        {
            new SchedulingController().DeleteSchedule(objScheduleItem.ScheduleID);
            Scheduler.CoreScheduler.RemoveFromScheduleQueue(objScheduleItem);
            DataCache.RemoveCache("ScheduleLastPolled");
        }

        public override void ExecuteTasks()
        {
            if (SchedulingProvider.Enabled)
            {
                Scheduler.CoreScheduler s = new Scheduler.CoreScheduler(SchedulingProvider.Debug, SchedulingProvider.MaxThreads);
                Scheduler.CoreScheduler.KeepRunning = true;
                Scheduler.CoreScheduler.KeepThreadAlive = false;
                Scheduler.CoreScheduler.Start();
            }
        }

        public override int GetActiveThreadCount()
        {
            SchedulingController s = new SchedulingController();
            return s.GetActiveThreadCount();
        }

        public override int GetFreeThreadCount()
        {
            SchedulingController s = new SchedulingController();
            return s.GetFreeThreadCount();
        }

        public override int GetMaxThreadCount()
        {
            SchedulingController s = new SchedulingController();
            return s.GetMaxThreadCount();
        }

        public override ScheduleItem GetNextScheduledTask(string Server)
        {
            SchedulingController s = new SchedulingController();
            return s.GetNextScheduledTask(Server);
        }

        public override string GetProviderPath()
        {
            return this.ProviderPath;
        }

        public override ArrayList GetSchedule()
        {
            SchedulingController s = new SchedulingController();
            return s.GetSchedule();
        }

        public override ScheduleItem GetSchedule(int ScheduleID)
        {
            SchedulingController s = new SchedulingController();
            return s.GetSchedule(ScheduleID);
        }

        public override ArrayList GetSchedule(string Server)
        {
            SchedulingController s = new SchedulingController();
            return s.GetSchedule(Server);
        }

        public override ScheduleItem GetSchedule(string TypeFullName, string Server)
        {
            SchedulingController s = new SchedulingController();
            return s.GetSchedule(TypeFullName, Server);
        }

        public override ArrayList GetScheduleHistory(int ScheduleID)
        {
            SchedulingController s = new SchedulingController();
            return s.GetScheduleHistory(ScheduleID);
        }

        public override Hashtable GetScheduleItemSettings(int ScheduleID)
        {
            SchedulingController s = new SchedulingController();
            return s.GetScheduleItemSettings(ScheduleID);
        }

        public override Collection GetScheduleProcessing()
        {
            SchedulingController s = new SchedulingController();
            return s.GetScheduleProcessing();
        }

        public override Collection GetScheduleQueue()
        {
            SchedulingController s = new SchedulingController();
            return s.GetScheduleQueue();
        }

        public override ScheduleStatus GetScheduleStatus()
        {
            SchedulingController s = new SchedulingController();
            return s.GetScheduleStatus();
        }

        public override void Halt(string SourceOfHalt)
        {
            Scheduler.CoreScheduler s = new Scheduler.CoreScheduler(SchedulingProvider.Debug, SchedulingProvider.MaxThreads);
            Scheduler.CoreScheduler.Halt(SourceOfHalt);
            Scheduler.CoreScheduler.KeepRunning = false;
        }

        public override void PurgeScheduleHistory()
        {
            Scheduler.CoreScheduler s = new Scheduler.CoreScheduler(SchedulingProvider.MaxThreads);
            Scheduler.CoreScheduler.PurgeScheduleHistory();
        }

        public override void ReStart(string SourceOfRestart)
        {
            this.Halt(SourceOfRestart);
            this.StartAndWaitForResponse();
        }

        public override void RunEventSchedule(EventName objEventName)
        {
            if (SchedulingProvider.Enabled)
            {
                Scheduler.CoreScheduler s = new Scheduler.CoreScheduler(SchedulingProvider.Debug, SchedulingProvider.MaxThreads);
                Scheduler.CoreScheduler.RunEventSchedule(objEventName);
            }
        }

        public override void Start()
        {
            if (SchedulingProvider.Enabled)
            {
                Scheduler.CoreScheduler s = new Scheduler.CoreScheduler(SchedulingProvider.Debug, SchedulingProvider.MaxThreads);
                Scheduler.CoreScheduler.KeepRunning = true;
                Scheduler.CoreScheduler.KeepThreadAlive = true;
                Scheduler.CoreScheduler.Start();
            }
        }

        public override void StartAndWaitForResponse()
        {
            if (SchedulingProvider.Enabled)
            {
                int endLoopIntHelperS0;
                Thread newThread = new Thread(new ThreadStart(this.Start));
                newThread.IsBackground = true;
                newThread.Start();
                int i = 0;
                do
                {
                    if (this.GetScheduleStatus() != ScheduleStatus.STOPPED)
                    {
                        break;
                    }
                    Thread.Sleep(0x3e8);
                    i++;
                    endLoopIntHelperS0 = 30;
                }
                while (i <= endLoopIntHelperS0);
            }
        }

        public override void UpdateSchedule(ScheduleItem objScheduleItem)
        {
            Scheduler.CoreScheduler.RemoveFromScheduleQueue(objScheduleItem);
            new SchedulingController().UpdateSchedule(objScheduleItem.ScheduleID, objScheduleItem.TypeFullName, objScheduleItem.TimeLapse, objScheduleItem.TimeLapseMeasurement, objScheduleItem.RetryTimeLapse, objScheduleItem.RetryTimeLapseMeasurement, objScheduleItem.RetainHistoryNum, objScheduleItem.AttachToEvent, objScheduleItem.CatchUpEnabled, objScheduleItem.Enabled, objScheduleItem.ObjectDependencies, objScheduleItem.Servers);
            ScheduleHistoryItem objScheduleHistoryItem = new ScheduleHistoryItem(objScheduleItem);
            if (((((objScheduleHistoryItem.TimeLapse != Null.NullInteger) && (objScheduleHistoryItem.TimeLapseMeasurement != Null.NullString)) && objScheduleHistoryItem.Enabled) ? 1 : 0) != 0)
            {
                objScheduleHistoryItem.ScheduleSource = ScheduleSource.STARTED_FROM_SCHEDULE_CHANGE;
                Scheduler.CoreScheduler.AddToScheduleQueue(objScheduleHistoryItem);
            }
            DataCache.RemoveCache("ScheduleLastPolled");
        }
    }
}

